package org.dusin.wufang.util;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;


public class CreateDB {

    public static void main(String[] args) {
        try { // load the driver 
            Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
            System.out.println("Load the embedded driver");
            Connection conn = null;
            Properties props = new Properties();
            props.put("user", "admin");
            props.put("password", "admin");
//create and connect the database named helloDB 
            conn = DriverManager.getConnection("jdbc:derby:WuFang;create=true", props);
            System.out.println("create and connect to WuFang");
            conn.setAutoCommit(false);

// create a table and insert two records 
            Statement s = conn.createStatement();
            //s.execute("drop table visitor");
            s.execute("create table visitor("
                    + "id INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),loginName varchar(30),"
                    + "password varchar(30),employeeNo char(15),realName varchar(30),createdTime timestamp,level smallint,contact varchar(50),note varchar(30))");
            
            System.out.println("table visitor created");
            s.execute("insert into visitor(loginName,password) values('admin', 'admin')");
          
// list the two records 
            
// delete the table 
            //s.execute("drop table hellotable");
            //System.out.println("Dropped table hellotable");

            
            s.close();
            System.out.println("Closed result set and statement");
            conn.commit();
            conn.close();
            System.out.println("Committed transaction and closed connection");

            try { // perform. a clean shutdown 
                DriverManager.getConnection("jdbc:derby:;shutdown=true");
            } catch (SQLException se) {
                System.out.println("Database shut down normally");
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
        System.out.println("SimpleApp finished");
    }
}
